<?php
namespace App\Models;

use DB;
use Uuid;
use App\Models\FavorM;
use App\Models\ImageM;
use App\Models\UserM;
use  Illuminate\Database\Eloquent\Model;

/**
* icon的模型
* @date: 2016年10月20日 下午8:13:39
* @author: lee
*/
class IconM extends Model
{
    protected $table='lee_icon';
    static public $STable='lee_icon';
    
    // set table's primary key to UUID
   // public $incrementing=false;
    public $primaryKey='fid';
    
       
    /**
     * 添加数据记录到 icon 数据表
     * @param array $data 待添加数据
     * 如 array('userid'=>userid,'description'=>discription,'id'=>iconid)
     */
    static public function addItem(array $data){
        $iconInst=new IconM();
        foreach ($data as $key => $value){
            $iconInst->$key=$value;
        }
        $iconInst->save();
       return $iconInst->fid;
    }
    
    
    /**
     * 依据 icon的fid 返回其 id
     * @param bigint $iconfid
     */
    static public function getID($iconfid){
        $iconInst= self::where('fid','=',$iconfid)->first();
        return ['icnid'=>$iconInst->id,'userid'=>$iconInst->userid];
    }

    /**
     * 依据uuid删除单个数据记录
     * @param uuid $id,待删除数据的主键
     */
    static public function deltItem($id){
        IconM::destroy($id);
    }
    
    /**
     * 依据icon的fid删除单个数据记录
     * @param bigint $icnfid,待删除icon的fid
     */
    static public function deltItembyFid($icnfid){
        IconM::where('fid','=',$icnfid)->delete();
    }
    
    /**
     * 依据 icon的fid 自增 被点赞数
     * @param int $iconfid ，icon的fid 值
     * @return bool
     */
    static public function plusFavourNum($iconfid){
        $tbName=IconM::$STable;
        $sql="update $tbName set beliked=beliked+1 where fid= $iconfid";
        return DB::statement($sql);
    }
    

    /**
     * 依据 icon的fid集合 批量自增 被点赞数
     * @param array $iconfids ，icon的fid集合
     * 如： array(iconfid1,iconfid2,iconfid3)
     * @return bool
     */
    static public function plusFavourNumBatch(array $iconfids){
        $tbName=IconM::$STable;
        $iconfidstr=implode(',', $iconfids);
        $sql="update $tbName set beliked=beliked+1 where fid in ( $iconfidstr )";
        return DB::statement($sql);
    }
    
    
    /**
     * 依据 icon的fid 自减  被点赞数
     * @param int $iconfid ，icon的fid 值
     * @return bool
     */
    static public function minFavourNum($iconfid){
        $tbName=IconM::$STable;
        $sql="update $tbName set beliked=beliked-1 where fid= $iconfid";
        return DB::statement($sql);
    }
    
    
    /**
     * 依据 icon的fid集合 批量自减  被点赞数
     * @param array $iconfids ，icon的fid集合
     * 如 array(iconfid1,iconfid2,iconfid3)
     * @return bool
     */
    static public function minfavourNumBatch(array $iconfids){
        $tbName=IconM::$STable;
        $iconfidstr=implode(',', $iconfids);
        $sql="update $tbName set beliked=beliked-1 where fid in ( $iconfidstr)";
        return DB::statement($sql);
    }
    

    /**
     * 依据 icon的fid 自增 被点收藏数
     * @param int $iconfid ，icon的fid 值
     * @return bool
     */
    static public function plusStoreNum($iconfid){
        $tbName=IconM::$STable;
        $sql="update $tbName set bestored=bestored+1 where fid= $iconfid";
        return DB::statement($sql);
    
    }
    
    
    /**
     * 依据 icon的fid集合 批量自增 被点收藏数
     * @param array $iconfids ，icon的fid 值
     * @return bool
     */
    static public function plusStoreNumBatch(array $iconfids){
        $tbName=IconM::$STable;
        $iconfidstr=implode(',', $iconfids);
        $sql="update $tbName set bestored=bestored+1 where fid in ( $iconfidstr)";
        return DB::statement($sql);
    
    }
    
    /**
     * 依据 icon的fid 自减  被收藏数
     * @param int $iconfid ，icon的fid 值
     * @return bool 
     */
    static public function minStoreNum($iconfid){
        $tbName=IconM::$STable;
        $sql="update $tbName set bestored=bestored-1 where fid= $iconfid";
        return DB::statement($sql);
    }
    
    /**
     * 依据 icon的fid集合 批量 自减  被收藏数
     * @param array $iconfids ，icon的fid集合
     * @return bool
     */
    static public function minStoreNumBatch(array $iconfids){
        $tbName=IconM::$STable;
        $iconfidstr=implode(',', $iconfids);
        $sql="update $tbName set bestored=bestored-1 where fid in ( $iconfidstr )";
        return DB::statement($sql);
    }
    
    
    /**
     * 依据 icon的fid集合 批量 自加  被浏览数
     * @param array $iconfids ，icon的fid集合
     * @return bool
     */
    static public function plusBrowseNumBatch(array $iconfids){
        $tbName=IconM::$STable;
        $iconfidstr=implode(',', $iconfids);
        $sql="update $tbName set bebrowsed=bebrowsed+1 where fid in ( $iconfidstr )";
        return DB::statement($sql);
    }
    
    /**
     * 浏览数 ++
     * @param unknown $icnfid
     */
    static  public  function plusBrowseNum($icnfid){
        self::where('fid',$icnfid)->increment('bebrowsed',1);
    }
    
    /**
     * 加载 所有icon及其相关的数据
     * @param bigint $userid 用户id
     * 返回项包括：
     *  icon的 fid,title,content,beliked(被点赞数),bestored（被收藏数）,
     *  lng（经度）,lat（纬度）,address（地点）,createdat(创建时间)
     *  favourstate（是否已被该用户点赞）,storedstate（是否已被该用户收藏）
     *  user的 id,name,avatar
     *  image的fid,path
     * 
     */
    static public function loadAllIcons($userid){
        $icnTable=self::$STable;
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;

        $sql="select icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,icn.des_cn as content_cn,
            icn.beliked as likenum,icn.bebrowsed as browsenum,icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
            icn.address as address,icn.created_at as createdat,
            img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgWd ,img.height as imgHt,
            userp.id as userid,userp.name as username,userp.avatar as useravatar,
            fv.iconfid as favourstate,st.iconfid as storedstate
            FROM $icnTable as icn 
            LEFT OUTER JOIN  $userTable as userp  
            ON icn.userid=userp.id 
            LEFT OUTER JOIN $imgTable as img 
            ON icn.id=img.iconid 

	       LEFT OUTER JOIN $favourTable as fv
    	   ON icn.fid=fv.iconfid and fv.userid = $userid
    
	       LEFT OUTER JOIN $storeTable as st
    	   ON icn.fid=st.iconfid and st.userid=$userid
           where icn.ischecked = true ";
        
        return DB::select($sql);
    }
    
    
    /** 
     * // store function
     * 分页 加载 icon及其相关的数据
     * 返回项包括：
     * @param int $limit,返回最大记录数
     * @param int $offset，偏移量
     *  icon的 fid,title,content,beliked(被点赞数),bestored（被收藏数）,
     *  lng（经度）,lat（纬度）,address（地点）,createdat(创建时间),
     *  favourstate（是否已被该用户点赞）,storedstate（是否已被该用户收藏）,
     *  user的 id,name,avatar
     *  image的fid,path
     *
     */
    static public function loadAllIconsByPages($userid,$limit=50,$offset=0){
        $icnTable=self::$STable;
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;
        // 依据 limit 和 offset 检索 icon记录        
        $sql="select icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,icn.des_cn as content_cn,
            icn.beliked as likenum,icn.bebrowsed as browsenum,icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
            icn.address as address,icn.created_at as createdat,
            img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgWd ,img.height as imgHt,
            userp.id as userid,userp.name as username,userp.avatar as useravatar,
            fv.iconfid as favourstate,st.iconfid as storedstate
            FROM
            $icnTable as icn
            LEFT OUTER JOIN  $userTable as userp
            ON icn.userid=userp.id
            LEFT OUTER JOIN $imgTable as img
            ON icn.id=img.iconid
            
	       LEFT OUTER JOIN $favourTable as fv
    	   ON icn.fid=fv.iconfid and fv.userid = $userid
    
	       LEFT OUTER JOIN $storeTable as st
    	   ON icn.fid=st.iconfid and st.userid=$userid
            
           where icn.id in
            
           ( select id from $icnTable  where ischecked = true limit $limit offset $offset )";
        return DB::select($sql);
    }
    
    /**
     * 依据icon的fid值 返回 小于该fid值的 限制条数记录
     * @param unknown $userid
     * @param number $limit
     * @param number $minFid
     */
    static public function loadAllIconsByMinFid($userid,$limit=50,$minFid=0){
        $icnTable=self::$STable;
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;
        $userFollow=UserFollowM::$STable;

        // 依据 limit 和 offset 检索 icon记录
        $sql="select icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,icn.des_cn as content_cn,
        icn.beliked as likenum,icn.bebrowsed as browsenum,icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
        icn.address as address,icn.created_at as createdat,
        img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgWd ,img.height as imgHt,
        userp.id as userid,userp.name as username,userp.avatar as useravatar,
        userp.height as height, userp.weight as weight,userp.underbustgirth as bust,
        userp.bra as bra ,userp.address as uaddress,
        fv.iconfid as favourstate,st.iconfid as storedstate,
        userf.userid as followstate
        
        FROM
        
        $icnTable as icn
        LEFT OUTER JOIN  $userTable as userp
        ON icn.userid=userp.id
        
        LEFT OUTER JOIN  $userFollow as userf
        ON userf.userid=$userid and  icn.userid=userf.followid
        
        LEFT OUTER JOIN $imgTable as img
        ON icn.id=img.iconid
        
        LEFT OUTER JOIN $favourTable as fv
        ON icn.fid=fv.iconfid and fv.userid = $userid
        
        LEFT OUTER JOIN $storeTable as st
        ON icn.fid=st.iconfid and st.userid=$userid
        
        where icn.id in
        
        ( select id from $icnTable where fid < $minFid  and ischecked = true 
        order by fid DESC limit $limit offset 0 ) 
        
        order by icn.fid DESC,img.fid ASC ";
        return DB::select($sql);
    }
    
    /**
     * 
     * @param int $userid, 用户id
     * @param string $fashionStyles,icon的fashion
     * @param int $limit,最大返回记录
     * @param int $offset,偏移量
     */
    static public function loadIconsByFashion($userid, $fashionStyle,$limit=50,$offset=0){
        $icnTable=self::$STable;
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;
        $userFollow=UserFollowM::$STable;
        // 依据 limit 和 offset 检索 icon记录
        $sql="select icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,icn.des_cn as content_cn,
        icn.beliked as likenum,icn.bebrowsed as browsenum,icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
        icn.address as address,icn.created_at as createdat,
        img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgwd ,img.height as imght,
        userp.id as userid,userp.name as username,userp.avatar as useravatar,
        userp.height as height, userp.weight as weight,userp.underbustgirth as bust,
        userp.bra as bra ,userp.address as uaddress,
        fv.iconfid as favourstate,st.iconfid as storedstate,
        userf.userid as followstate
        
        FROM
        
        $icnTable as icn
        LEFT OUTER JOIN  $userTable as userp
        ON icn.userid=userp.id
        
        LEFT OUTER JOIN  $userFollow as userf
        ON userf.userid=$userid and  icn.userid=userf.followid
        
        LEFT OUTER JOIN $imgTable as img
        ON icn.id=img.iconid
    
        LEFT OUTER JOIN $favourTable as fv
        ON icn.fid=fv.iconfid and fv.userid = $userid
    
        LEFT OUTER JOIN $storeTable as st
        ON icn.fid=st.iconfid and st.userid=$userid
    
        where icn.id in
    
        ( select id from $icnTable where fashion_style like '%$fashionStyle%'  
            and ischecked = true 
        order by created_at DESC 
        limit $limit offset $offset )";
        return DB::select($sql);
    }
    
    
    
    /**
     * // store function
     * 分页 加载 最新 上传的icon及其相关的数据
     * 返回项包括：
     * @param int $limit,返回最大记录数
     * @param int $offset，偏移量
     *  icon的 fid,title,content,beliked,bestored,
     *  user的 id,name,avatar
     *  image的fid,path
     *
     */
    static public function loadAllNewestIconsByPages($userid,$limit=50,$offset=0){
        $icnTable=self::$STable;
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;
        $userFollow=UserFollowM::$STable;
        
        // 依据 limit 和 offset 检索 最新的icon记录
        $sql="select icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,icn.des_cn as content_cn,
        icn.beliked as likenum,icn.bebrowsed as browsenum,icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
        icn.address as address,icn.created_at as createdat,
        img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgwd ,img.height as imght,
        userp.id as userid,userp.name as username,userp.avatar as useravatar,
        userp.height as height, userp.weight as weight,userp.underbustgirth as bust,
        userp.bra as bra ,userp.address as uaddress,
        fv.iconfid as favourstate,st.iconfid as storedstate,
        userf.userid as followstate
        
        FROM
        $icnTable as icn
        
        LEFT OUTER JOIN  $userTable as userp
        ON icn.userid=userp.id
        
        LEFT OUTER JOIN  $userFollow as userf
        ON userf.userid=$userid and  icn.userid=userf.followid
        
        LEFT OUTER JOIN $imgTable as img
        ON icn.id=img.iconid
        
        LEFT OUTER JOIN $favourTable as fv
        ON icn.fid=fv.iconfid and fv.userid = $userid
        
        LEFT OUTER JOIN $storeTable as st
        ON icn.fid=st.iconfid and st.userid=$userid
        
        where icn.id in
        
        ( select id from $icnTable  where ischecked = true  order by fid DESC limit $limit offset $offset )
         order by icn.fid DESC,img.fid ASC ";
        return DB::select($sql);
    }
    
    
    /**
     * 依据 用户id和日期 返回 当月上传日 iconfid的集合
     * @param unknown $userid
     * @param unknown $date
     */
    static public function loadIconInMonth($userid,$date){
        $icnTable=self::$STable;
        $sql="select extract(day from created_at) as cdate,icn.fid as iconfid from $icnTable as icn 
        where userid = $userid 
        and extract(month from created_at) = extract(month from date('$date'))
        and extract(year from created_at) = extract(year from date('$date'))";
        return DB::select($sql);
    }
    
    /**
     * 限制返回 其他用户的 icon记录
     * @param unknown $userid
     * @param unknown $limit
     * @param unknown $offset
     */
    static public function loadOthersIcons($beloadedUserid,$userid,$limit,$offset){
        $icnTable=self::$STable;
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;
        $userFollow=UserFollowM::$STable;

        // 依据 limit 和 offset 检索 最新的icon记录
        $sql="select
        icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,
        icn.des_cn as content_cn,
        icn.beliked as likenum,icn.bebrowsed as browsenum,
        icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
        icn.address as address,icn.created_at as createdat,
        img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgwd ,img.height as imght,
       
        fv.iconfid as favourstate,st.iconfid as storedstate,
        userf.userid as followstate
        
        FROM
        $icnTable as icn
        
        LEFT OUTER JOIN  $userFollow as userf
        ON userf.userid=$userid and  icn.userid=userf.followid
        
        LEFT OUTER JOIN $imgTable as img
        ON icn.id=img.iconid
        
        LEFT OUTER JOIN $favourTable as fv
        ON icn.fid=fv.iconfid and fv.userid = $userid
        
        LEFT OUTER JOIN $storeTable as st
        ON icn.fid=st.iconfid and st.userid=$userid
        
        where icn.id in
        
        ( select id from $icnTable where userid= $beloadedUserid 
         and ischecked = true 
        order by fid DESC limit $limit offset $offset )
        order by icn.fid DESC,img.fid ASC ";
        return DB::select($sql);
 
    }
    
    /**
     * 依据用户id 返回 分页返回该用户的 icon记录
     * @param unknown $userid
     * @param unknown $limit
     * @param unknown $offset
     */
    static public function loadOwnIcon($userid,$limit,$offset){
        $icnTable=self::$STable;
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;

        // 依据 limit 和 offset 检索 最新的icon记录
        $sql="select icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,icn.des_cn as content_cn,
        icn.beliked as likenum,icn.bebrowsed as browsenum,icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
        icn.address as address,icn.created_at as createdat,
        img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgwd ,img.height as imght,
        userp.id as userid,userp.name as username,
        userp.name as username,userp.avatar as useravatar,
        userp.height as height, userp.weight as weight,userp.underbustgirth as bust,
        userp.bra as bra ,userp.address as uaddress,
        fv.iconfid as favourstate,st.iconfid as storedstate
        FROM
        
        $icnTable as icn
        LEFT OUTER JOIN  $userTable as userp
        ON icn.userid=userp.id
        LEFT OUTER JOIN $imgTable as img
        ON icn.id=img.iconid
        
        LEFT OUTER JOIN $favourTable as fv
        ON icn.fid=fv.iconfid and fv.userid = $userid
        
        LEFT OUTER JOIN $storeTable as st
        ON icn.fid=st.iconfid and st.userid=$userid
        
        where icn.fid in 
        ( select fid from $icnTable where userid =$userid order by fid ASC limit $limit offset $offset )";
        return DB::select($sql);
    }
    
    /**
     * 依据用户id和 icon的fid集合 返回icon信息
     * @param unknown $userid
     * @param array $fids
     */
    static public function loadIconInFids($userid,array $fids){
        $icnTable=self::$STable;
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;
        $userFollow=UserFollowM::$STable;
        
        $iconFids=implode(',', $fids);

        // 依据 limit 和 offset 检索 最新的icon记录
        $sql="select icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,icn.des_cn as content_cn,
        icn.beliked as likenum,icn.bebrowsed as browsenum,icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
        icn.address as address,icn.created_at as createdat,
        img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgwd ,img.height as imght,
        userp.id as userid,userp.name as username,userp.avatar as useravatar,
        userp.height as height, userp.weight as weight,userp.underbustgirth as bust,
        userp.bra as bra ,userp.address as uaddress,
        fv.iconfid as favourstate,st.iconfid as storedstate,
        userf.userid as followstate
        
        FROM
        $icnTable as icn
        
        LEFT OUTER JOIN  $userTable as userp
        ON icn.userid=userp.id
        
        LEFT OUTER JOIN  $userFollow as userf
        ON userf.userid=$userid and  icn.userid=userf.followid
        
        LEFT OUTER JOIN $imgTable as img
        ON icn.id=img.iconid
        
        LEFT OUTER JOIN $favourTable as fv
        ON icn.fid=fv.iconfid and fv.userid = $userid
        
        LEFT OUTER JOIN $storeTable as st
        ON icn.fid=st.iconfid and st.userid=$userid
        where icn.fid in ( $iconFids )  ";
        return DB::select($sql);
    }
    
    

    /**
     * 依据user的id和icon的fid 返回其 被点赞和收藏的状态
     * @param bigint $userid,user的id值
     * @param bigint $iconfid,icon的fid值
     * 若有被收藏 或 被点赞 其返回内容不为空
     */
    static public function iconFavorStoreState($userid,$iconfid){
        $favorT=FavorM::$STable;
        $storeT=StoreM::$STable;
        $iconT=self::$STable;
        $sql="select ic.fid as iconfid,fv.userid as fvuseid,st.userid as stuserid
         from $iconT as ic
         left  join $favorT as fv
         on ic.fid=fv.iconfid and fv.userid=$userid
         left  join $storeT as st
         on ic.fid=st.iconfid and st.userid=$userid
         where ic.fid=$iconfid";
        return DB::select($sql);
    }
    
    
    /**
     * 依据user的id和icon的fids 返回其 被点赞和收藏的状态
     * @param bigint $userid,user的id值
     * @param array $iconfids,icon的fids集合
     * 若有被收藏 或 被点赞 其返回内容不为空
     */
    static public function iconsFavorStoreState($userid,array $iconfids){
        $favorT=FavorM::$STable;
        $storeT=StoreM::$STable;
        $iconT=self::$STable;
        $iconFidStr=implode(',', $iconfids);
        $sql="select ic.fid as iconfid,fv.userid as fvuseid,st.userid as stuserid
        from $iconT as ic
        left  join $favorT as fv
        on ic.fid=fv.iconfid and fv.userid=$userid
        left  join $storeT as st
        on ic.fid=st.iconfid and st.userid=$userid
        where ic.fid in ( $iconFidStr )";
        return DB::select($sql);
    }
    
    /**
     * 依据 icon的fid 返回和该fid有关的 image表id(uuid)、存储路径,和 comment表的 id
     * @param bigint $icnfid, icon的fid
     * @return 数组  [ 'imgids'=>[img1,img2],'imgpaths'=>[path1,path2],'cmtids'=>[cmt1,cmt2] ]
     */
    static  public function getImgAndCmtIDs($icnfid){
         $imgT=ImageM::$STable;
         $cmtT=CommentM::$STable;
         $icnT=self::$STable;
        
        $sql="select img.id as imgids,img.path as imgpath,cmt.id as cmtids
            from $imgT as img
            left outer join $cmtT as cmt
            on cmt.iconfid=$icnfid
            where img.iconid= 
                (select icn.id from $icnT as icn where icn.fid=$icnfid)";
        $sqlR=DB::select($sql);
        $temp=array();
       foreach ($sqlR as $oneItem){
           array_push(  $temp, ['imgids'=>$oneItem->imgids,
               'imgpath'=>$oneItem->imgpath,'cmtids'=>$oneItem->cmtids
           ]);
       }
        return ['imgids'=>array_unique(array_column($temp, 'imgids')),
                 'imgpaths'=>array_unique(array_column($temp, 'imgpath')),
                 'cmtids'=>array_unique(array_column($temp, 'cmtids'))
                 ];
    }
    
    /**
     * 返回 userids 集合中所有相关联的 icon信息，并附加  loadUser状态信息
     * @param unknown $loadUsrid
     * @param array $userids
     */
    static public function loadIconsInUserids($loadUsrid,array $userids){
        
        $icnTable=self::$STable;
        $useridColl=implode(',', $userids);
        $imgTable=ImageM::$STable;
        $userTable=UserM::$STable;
        $favourTable=FavorM::$STable;
        $storeTable=StoreM::$STable;

        $sql="select 
        
        icn.userid as userid,icn.fid as icnfid, icn.title as title,icn.title_cn as title_cn,icn.description as content,icn.des_cn as content_cn,
        icn.beliked as likenum,icn.bebrowsed as browsenum,icn.bestored as storenum ,icn.lng as lng,icn.lat as lat,
        icn.address as address,icn.created_at as createdat,
        img.fid as imgfid,img.path as imglink ,img.thumb_path as thumb_imglink,img.width as imgwd ,img.height as imght,
        fv.iconfid as favourstate,st.iconfid as storedstate
        FROM
        $icnTable as icn
        LEFT OUTER JOIN $imgTable as img
        ON icn.id=img.iconid
        
        LEFT OUTER JOIN $favourTable as fv
        ON icn.fid=fv.iconfid and fv.userid = $loadUsrid
        
        LEFT OUTER JOIN $storeTable as st
        ON icn.fid=st.iconfid and st.userid=$loadUsrid
        
        where icn.userid in ( $useridColl )  order by icn.fid ASC";
       return DB::select($sql);
    }
    
}

?>